//
//  NSObject+TestModel.m
//  RuntimeDemo
//
//  Created by 张敬 on 2019/10/14.
//  Copyright © 2019年 xiuxiu. All rights reserved.
//

#import "NSObject+TestModel.h"
#import <objc/runtime.h>

@implementation NSObject (TestModel)

+ (NSArray *)propertyList{
    
    NSMutableArray *array = [NSMutableArray array];
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList([self class], &count);
    for (unsigned int k = 0; k < count; k++) {
        const char *propertyName = property_getName(propertyList[k]);
        NSString *key = [NSString stringWithUTF8String:propertyName];
        [array addObject:key];
    }
    free(propertyList);
    return array.copy;
}


+ (id)modelWithDictory:(NSDictionary *)dic{
    
    id model = [self new];
    for (NSString *key in [self propertyList]) {
        id value = dic[key];
        if (value) {
            [model setValue:value forKey:key];
        }
    }
    return model;
}

+ (NSDictionary *)dictoryWithModel:(id)model{
    
    NSMutableDictionary *aimDic = [NSMutableDictionary dictionary];
    for (NSString *key in [self propertyList]) {
        id value = [model valueForKey:key];
        if (value) {
            [aimDic setObject:value forKey:key];
        }
    }
    return aimDic.copy;
}

@end
